﻿var ns = namespace("kaskelot.sp.ui");

ns.smsform = function () {

	function init() {
		$("#sms-text").on("input", function (e) {
			ensureMessageSize();
		});

		$("#sms-text").on("keyup", function (e) {
			ensureMessageSize();
		});

		$("#sms-text").on("paste", function (e) {
			ensureMessageSize();
		});
	}

	function getSelectedValue(dataTag) {
		var val = [];
		$("#search-results tbody input:checked").each(function (i) {
			val[i] = $(this).attr("data-" + dataTag);
		});

		return val;
	}

	function ensureMessageSize() {
		var maxLength = 160;

		setTimeout(function () {
			if ($("#sms-text").val().length > maxLength) {
				$("#sms-text").val($("#sms-text").val().substring(0, maxLength));
			}

			$("#charCounter").html($("#sms-text").val().length);
		}, 0);
	}

	function showSMSForm() {
		var ktId = getParameterByName("KurstillfalleId");
		var selector = "#sms-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "SMSForm",
				parentIdString: ktId
			},
			selector: selector,
			openFunction: function () {

			},
			okFunction: function () {
				if (confirm("Är du säker på att du vill skicka påminnelsen?")) {
					$("#sms-form").block("Skickar...");

					$.ajax({
						url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/SkickaSMS",
						type: "GET",
						data: { ktId: ktId, text: $("#sms-text").val() },
						processData: true,
						cache: false,
						success:
							function (data) {
								if (!data.SkickaSMSResult.HasError) {
									$(selector).dialog("close");
								} else {
									kaskelot.sp.ui.prompts.showErrorPrompt(data.SkickaSMSResult.ErrorMessage);
									$(selector).dialog("close");
								}
							},
						error:
							function (xhr) {
								kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
								$(selector).dialog("close");
							}
					});
				}
			}
		});
	}

	function showSMSFormForSelected() {
		var recipients = getSelectedValue("mobtelnr");
		if (recipients.length == 0)
		{
			kaskelot.sp.ui.prompts.showErrorPrompt("Du måste först välja de personer du vill skicka SMS till.");
			return;
		}

		var selector = "#sms-form";
		kaskelot.sp.ui.formutils.getAndShowPostForm({
			params: {
				formName: "SMSForm",
				idList: recipients
			},
			selector: selector,
			openFunction: function () {

			},
			okFunction: function () {
				if (confirm("Är du säker på att du vill skicka påminnelsen?")) {
					$(selector).block("Skickar...");

					$.ajax({
						url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/SkickaSMS",
						type: "GET",
						data: { ktId: "", mobtelnrList: $("#Id").val(), text: $("#sms-text").val() },
						processData: true,
						cache: false,
						success:
							function (data) {
								if (!data.SkickaSMSResult.HasError) {
									$(selector).dialog("close");
								} else {
									kaskelot.sp.ui.prompts.showErrorPrompt(data.SkickaSMSResult.ErrorMessage);
									$(selector).dialog("close");
								}
							},
						error:
							function (xhr) {
								kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
								$(selector).dialog("close");
							}
					});
				}
			}
		});
	}

	return {
		init: init,
		showSMSForm: showSMSForm,
		showSMSFormForSelected: showSMSFormForSelected
	};
}();